/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.kj;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import java.util.Queue;
import org.apache.log4j.Logger;



/**
 *
 * @author Admin
 */
public abstract class Collector {
    private static Logger logger = Logger.getLogger(Collector.class.getName());
    
    protected CommPortIdentifier commPortId;   
    protected SerialPort serialPort;
    protected OutputStream outputStream;
    protected InputStream inputStream;
    
    protected String portId = "COM1"; 
    protected int address ;     
    
    protected int delay = 500;  //设备延时，读取数据    
    protected int no=1;   //设备编号
    
    protected Queue<Integer> historyQueue = new LinkedList<Integer>();    //最近10次采集数据的成功 与失败，为了得到采集数据是否正常
    protected int maxHistoryQueueLenth = 10;     //记录最近采集成功与否的最大次数
    protected double preTotal = -1;

    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }
    
    
    //获取当前采集数据的成功率
    public double getSuccRate(){
        double r=0;
        int goodTimes=0;
        for(Integer i:historyQueue){
            if(i==0)
                goodTimes++;
        }
        r = (double)goodTimes/historyQueue.size();
        return r;
    }
    
    //将当前是否成功的状态置入对列
    protected void addQueue(int s){
        if(historyQueue.size()>=maxHistoryQueueLenth){
            historyQueue.poll();
        }
        historyQueue.add(s);
    }
    
    public int getDelay() {
        return delay;
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }  

    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }

    public String getPortId() {
        return portId;
    }
    public void setPortId(String portId) {
        this.portId = portId;
    }    

    //获取仪表实时读数
    public abstract Map<String,Object> retrieve();
}
